package com.wtf.generator.controller;

/**
 * date: 2022/6/18 10:59
 * @author linhuanyou
 * @email 496558736@qq.com
 * @version 1.0
 */
import com.wtf.generator.common.sys.R;
import com.wtf.generator.common.utils.PageUtils;
import com.wtf.generator.model.dto.DsAddDTO;
import com.wtf.generator.model.dto.DsDelDTO;
import com.wtf.generator.model.dto.DsListDTO;
import com.wtf.generator.model.dto.DsPageDTO;
import com.wtf.generator.model.vo.DsVO;
import com.wtf.generator.service.DynamicRoutingDataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "数据源操作")
@RestController
@RequestMapping("/ds")
public class DynamicRoutingDataSourceController {

    @Autowired
    private DynamicRoutingDataSourceService dynamicRoutingDataSourceService;

    @ApiOperation(value = "获取数据源(分页)")
    @GetMapping("/page")
    public R<PageUtils> page(@Validated DsPageDTO dsPageDTO){
        PageUtils pageUtils = dynamicRoutingDataSourceService.page(dsPageDTO);
        return R.ok(pageUtils);
    }

    @ApiOperation(value = "获取数据源")
    @GetMapping("/list")
    public R<List<DsVO>> list(@Validated DsListDTO dsListDTO){
        List<DsVO> dataSources = dynamicRoutingDataSourceService.list(dsListDTO);
        return R.ok(dataSources);
    }

    @ApiOperation(value = "新增数据源")
    @PostMapping("/add")
    public R add(@RequestBody @Validated DsAddDTO dsAddDTO){
        DsVO dataSource = dynamicRoutingDataSourceService.add(dsAddDTO);
        return R.ok();
    }

    @ApiOperation(value = "删除数据源")
    @PostMapping("/del")
    public R remove(@RequestBody @Validated DsDelDTO dsDelDTO){
        dynamicRoutingDataSourceService.remove(dsDelDTO);
        return R.ok();
    }

    @ApiOperation(value = "测试连接")
    @PostMapping("/testConnect")
    public R testConnect(@RequestBody @Validated DsAddDTO dsAddDTO){
        Boolean tesResult = dynamicRoutingDataSourceService.testConnect(dsAddDTO);
        return tesResult ? R.ok() : R.error("连接失败，请检查主机、账号、密码、数据库是否正确");
    }
}
